package com.zmn.plat.admin.controller.contract.file.record;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.zmn.common.dto2.ResponseDTO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.contract.file.record.ContractFileRecord;

/**
 * 合同附件记录表
 *
 * @author linfeng
 *
 * @since 2020-05-09 11:41:45
 */
@RestController
@RequestMapping("/contract/file/record/")
public class ContractFileRecordModifyController extends ContractFileRecordBaseController {

    /**
     * 新增修改
     * 
     * @author linfeng
     * @param contractFileRecord
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping(value = "addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ContractFileRecord contractFileRecord) {
        Date now = DateUtil.getNow();

        Integer relateId = contractFileRecord.getRelateId();

        // contractFileRecord.setFirstLetter(InitialUtil.getFristEnName(contractFileRecord.getName()));

        try {
            if (relateId == null || relateId == 0) {
                contractFileRecord.setCreater(getMcStaffName(request));
                contractFileRecord.setCreateTime(now);
                contractFileRecordService.insert(contractFileRecord);
            } else {
                contractFileRecord.setUpdater(getMcStaffName(request));
                contractFileRecord.setUpdateTime(now);
                contractFileRecordService.updateByKey(contractFileRecord);
            }

            return ResponseDTO.success(Integer.toString(contractFileRecord.getRelateId()), "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("不能重复添加！", contractFileRecord.getFileSrc());
            return ResponseDTO.fail("数据已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改合同附件记录表失败,contractFileRecord:[%s],ex:[%s]", JSONObject.toJSONString(contractFileRecord),
                ex.getMessage()));
            return ResponseDTO.fail("操作失败！");
        }
    }

}
